<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Project;//方便new model
use App\Att;
use App\Bid;
use DB;

use Gregwar\Captcha\CaptchaBuilder;//验证码
use Session;//seddion
class ProController extends Controller
{
    //负责输出表单
    public function jie() {
      return view('woyaojiekuan');
    }
    //处理表单数据
    public function jiePost(Request $req) {
      // return 'insert into DB';

    //   $this->validate($req , [
    //     //age不能为空且只能为15,40,80
    //   'age'=>'required|in:15,40,80',
    //   'money'=>'required|digits_between:5,7',
    //   'mobile'=>'required|regex:/^1[3458]\d{9}$/',
    // ],
    // [
    //   'money.required'=>'借款不能为0',
    //   'money.digits_between'=>':attribute 必须介于 :min-:max之间',
    // ]
    //
    // );
#########
//校验验证码
$captcha = Session::get('milkcaptcha');
        if($req->captcha == $captcha){
            // $results = DB::select('select * from v9_admin where username = ? and password = ? ',array($username,$password));
            //
            // if(empty($results)){
            //     echo '用户名或密码错误';
            // }else{
            //     $userid=$results[0]->userid;
            //     Session::put('username',$username);
            //     Session::put('userid',$userid);

                // echo '登陆成功';
            // }
            echo '验证码正确,值为:'.$captcha;

        }else{
            echo '验码错误,值为:'.$captcha;

        }
exit();
#########
      $pro = new Project();//new model类 得到对象
      $att = new Att();
      //把收到的金额转成分
      //$req->user()//获得当前登录用户的对象--uid
      $pro->uid = $req->user()->uid;
      $pro->name = $req->user()->name;
      $pro->money = intval($req->money)*100;
      $pro->mobile = $req->mobile;
      $pro->age = $req->age;
      $pro->pubtime = time();
      $pro->save();//此时,$pro对应表中--刚刚新增的那一行
      //写附表atts
      $att->uid = $req->user()->uid;
      $att->pid = $pro->pid;//save()相当于插入一条数据就有了pid

      $att->save();
      echo 'ok';
    }

    public function pro($pid){
      $pros = Project::find($pid);
      return view('lijitouzi',['pros'=>$pros]);
    }
    //投资
    public function touzi(Request $req,$pid){
      // $bid = Bid::where('pid',$pid)->get();//修改的时候查询
      $bid = new Bid();//新输入的new
      $bid->money = $req->money*100;//输入的money为元转换成分
      $bid->uid = $req->user()->uid;
      $bid->pid = $pid;
      $bid->title = $req->title;
      $bid->pubtime = $req->input('pubtime',time());
      $bid->save();

      $pro = Project::find($pid);
      $pro->recive += $bid->money;
      $pro->save();
      if($pro->recive == $pro->money){
        $this->touziDown($pid);
      }

      return '购买成功';
    }
    protected function touziDown($pid){
      //1.修改项目状态为2, 还款/收益中
      $pro = Project::find($pid);
      $pro->status = 2;
      $pro->save();
      //2.为借款者,生成还款记录
      //算出每月还款额,每个月的利息加上每个月的本金
      $amount = ($pro->money * $pro->rate/1200)+($pro->money/$pro->hrange);

      $row = ['uid'=>$pro->uid , 'pid'=>$pro->pid,'title'=>$pro->title];
      $row['amount'] = $amount;
      $row['status'] = 0;

      $today = date('Y-m-d',time());
      //按月生成还款记录
      for($i=1 ; $i <= $pro->hrange ; $i++){
        $paydate = date('Y-m-d' , strtotime("+ $i months"));
        $row['paydate'] = $paydate;
        DB::table('hks')->insert($row);
      }
        //3.为投资者产生与收益内容表
      $bids = Bid::where('pid',$pro->pid)->get();
      $row = array();
      $row['pid'] = $pro->pid;
      $row['title'] = $pro->title;
      //收益截止的日期
      $row['enddate'] = date('Y-m-d',strtotime("+ {$pro->hrange} months"));

      foreach($bids as $v){
        $row['uid'] = $v->uid;
        $row['amount'] = $v->money * $pro->rate/36500;
        DB::table('tasks')->insert($row);
      }
    }


}
